package com.codejam2013.qualification;
import java.io.File;
import java.io.PrintStream;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;


public class C {

	static int nTest, n;
	static long a, b;
	static int MAX = 10000000;
	static long[] num = new long[MAX+1];
	
	public static void main(String[] args) throws Exception {
		Scanner scanner = new Scanner(new File("C-in.txt"));
		PrintStream ps = new PrintStream(new File("C-out.txt"));
		System.setOut(ps);
		n=0;
		for (long i=1;i<=MAX;++i) {
			if (ispalin(i) && ispalin(i*i)) {
				num[n++] = i*i;
//				System.out.println(num[n-1]);
			}
		}
		nTest = scanner.nextInt();
		for (int t=1;t<=nTest;++t) {
			a = scanner.nextLong();
			b = scanner.nextLong();
			int from = Arrays.binarySearch(num, 0, n, a);
			if (from < 0) {
				from = -from -1;
			}
			int to = Arrays.binarySearch(num, 0, n, b);
			if (to < 0) {
				to = -to-2;
			}
			int count = to - from + 1;
			System.out.println("Case #" + t + ": " + count);
		}

	}

	private static boolean ispalin(long l) {
		String s = l + "";
		int from = 0;
		int to = s.length() - 1;
		while (from < to) {
			if (s.charAt(from) != s.charAt(to)) {
				return false;
			}
			else {
				from++;
				to--;
			}
		}
		return true;
	}

}
